package org.fxcontainer.utils
{

	import com.developmentarc.core.datastructures.utils.HashTable;

	import flash.events.IEventDispatcher;

	import mx.events.ResourceEvent;
	import mx.resources.IResourceBundle;
	import mx.resources.IResourceManager;
	import mx.resources.ResourceBundle;
	import mx.resources.ResourceManager;

	import org.as3commons.logging.ILogger;
	import org.as3commons.logging.LoggerFactory;

	[ResourceBundle("localization")]
	public class Resource
	{
		public static const LOCALE:String="en_US";

		private static var logger:ILogger=LoggerFactory.getClassLogger(Resource);

		/**
		 * Holds loaded properties.
		 * @default
		 */
		private static var properties:HashTable=new HashTable();

		/**
		 * Available resource bundles.
		 * @default
		 */
		private static var resources:Array=new Array();

		/**
		 * Retrieves message from bundles for given key.
		 * @param key the key to find message.
		 * @return message
		 */
		public static function getString(key:String):String
		{
			var message:String="";
			logger.debug("Load property \"{0}\" from resource bundle.", key);

			var resourceManager:IResourceManager=ResourceManager.getInstance();
			message=properties.getItem(key);
			if (null == message)
			{
				var bundles:Array=resourceManager.getBundleNamesForLocale(LOCALE);
				var resourceLenght:int=bundles.length;
				for (var i:int=0; i < resourceLenght; i++)
				{
					message=resourceManager.getString(bundles[i], key);
					if (null != message)
					{
						properties.addItem(key, message);
						break;
					}
				}
			}

			if ("" == message)
			{
				logger.debug("Property with key \"{0}\" was not found.", key);
				message=key;
			}
			return message;
		}

		/**
		 * Adds additional resource bundle.
		 * @param resource the resource to add.
		 */
		public static function addResource(resource:String):void
		{
			var resourceManager:IResourceManager=ResourceManager.getInstance();
			var resourceBundle:IResourceBundle=resourceManager.getResourceBundle(LOCALE, resource);
			if (null == resourceBundle)
			{
				resourceBundle=new ResourceBundle(LOCALE, resource);
				resourceManager.addResourceBundle(resourceBundle);
				resourceManager.update();
			}
			if (null == resourceBundle)
			{
				logger.error("Resource bundle: {0} was not added.", resource);
			}
		}

		public static function loadResource(resourceURL:String):void
		{
			var resource:String=resourceURL.substring(resourceURL.lastIndexOf("/") + 1, resourceURL.length);
			var resourceBundle:ResourceBundle=new ResourceBundle(LOCALE, resource);
			if (null != resourceBundle)
			{
				var resourceManager:IResourceManager=ResourceManager.getInstance();
				var resourceLoader:IEventDispatcher=resourceManager.loadResourceModule(resourceURL);
				resourceLoader.addEventListener(ResourceEvent.COMPLETE, onLoadComplete);
				resourceLoader.addEventListener(ResourceEvent.ERROR, onError);

			}
		}

		private static function onLoadComplete(event:ResourceEvent):void
		{
			if (null != event)
			{
				logger.info("Resource bundle: {0} was loaded.", event.currentTarget);
			}
		}

		private static function onError(event:ResourceEvent):void
		{
			if (null != event)
			{
				logger.error("Resource bundle: was not loaded.", event.errorText);
			}
		}

	}
}